package com.inossem.allotin.utils;

import android.content.Context;

import com.inossem.allotin.db.AllotInBean;
import com.inossem.allotin.db.CacheBean;
import com.inossem.allotin.db.greendao.AllotInBeanDao;
import com.inossem.allotin.db.greendao.CacheBeanDao;
import com.inossem.allotin.db.greendao.DaoMaster;
import com.inossem.allotin.db.greendao.DaoSession;

import org.greenrobot.greendao.query.DeleteQuery;
import org.greenrobot.greendao.query.QueryBuilder;

import java.util.Date;
import java.util.List;

public class GreenDaoManager {

    private static GreenDaoManager single = null;
    private static DaoMaster daoMaster;
    private static DaoSession daoSession;
    private Context context;
    private CacheBeanDao cacheBeanDao;
    private AllotInBeanDao allotInBeanDao;

    public synchronized static GreenDaoManager getInstance() {
        if (single == null) {
            single = new GreenDaoManager();
        }
        return single;
    }

    public void init(Context context) {
        if (cacheBeanDao != null) {
            return;
        }
        this.context = context;
        cacheBeanDao = getDaoSession(context).getCacheBeanDao();
        allotInBeanDao = getDaoSession(context).getAllotInBeanDao();
        cacheBeanDao = daoSession.getCacheBeanDao();
    }

    public static DaoMaster getDaoMaster(Context context) {
        if (daoMaster == null) {
            DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "local");
            daoMaster = new DaoMaster(helper.getWritableDb());
        }
        return daoMaster;
    }

    public static DaoSession getDaoSession(Context context) {
        if (daoSession == null) {
            if (daoMaster == null) {
                daoMaster = getDaoMaster(context);
            }
            daoSession = daoMaster.newSession();
        }
        return daoSession;
    }

    //新增缓存信息
    public void addCacheBean(CacheBean bean) {
        cacheBeanDao.insertOrReplace(bean);
    }

    //修改缓存信息
    public void updateCacheBean(List<CacheBean> list) {
        cacheBeanDao.updateInTx(list);
    }

    //删除缓存信息
    public void delCacheBean(long id) {
        QueryBuilder<CacheBean> qb = cacheBeanDao.queryBuilder();
        DeleteQuery<CacheBean> bd = qb.where(CacheBeanDao.Properties.Id.eq(id)).buildDelete();
        bd.executeDeleteWithoutDetachingEntities();
    }

    //查询单条缓存信息
    public List<CacheBean> getCacheBean(String whRealCode, Integer receiptType, long id) {
        List<CacheBean> list = cacheBeanDao.queryBuilder().where(
                CacheBeanDao.Properties.WhCode.eq(whRealCode),
                CacheBeanDao.Properties.ReceiptType.eq(receiptType),
                CacheBeanDao.Properties.Id.eq(id)).list();
        return list;
    }

    //查询全部缓存信息
    public List<CacheBean> getCacheBeanList(String whRealCode, Integer receiptType) {
        List<CacheBean> list = cacheBeanDao.queryBuilder().where(CacheBeanDao.Properties.WhCode.eq(whRealCode),
                CacheBeanDao.Properties.ReceiptType.eq(receiptType)).list();
        return list;
    }

    //查询全部缓存信息
    public List<CacheBean> getCacheBeanList() {
        List<CacheBean> list = cacheBeanDao.queryBuilder().orderDesc(CacheBeanDao.Properties.Id).limit(1).list();
        return list;
    }

    //新增缓存信息
    public void addAllotInBean(AllotInBean bean, boolean isSave) {
        AllotInBean allotInBean = allotInBeanDao.queryBuilder().where(AllotInBeanDao.Properties.StockTransportCode.eq(bean.getStockTransportCode())).unique();
        if (allotInBean == null) {
            allotInBeanDao.insert(bean);
        } else {
            if (isSave || !allotInBean.getMTransportHead().isEdit()) {
                allotInBean.setCreateTime(bean.getCreateTime());
                allotInBean.setStockTransportCode(bean.getStockTransportCode());
                allotInBean.setMTransportHead(bean.getMTransportHead());
                allotInBeanDao.update(allotInBean);
            }
        }
    }

    //删除缓存信息
    public void deleteAllotInBean(String id) {
        QueryBuilder<AllotInBean> qb = allotInBeanDao.queryBuilder();
        DeleteQuery<AllotInBean> bd = qb.where(AllotInBeanDao.Properties.StockTransportCode.eq(id)).buildDelete();
        bd.executeDeleteWithoutDetachingEntities();
    }

    //查询全部缓存信息
    public List<AllotInBean> getAllotInBeanList(Date startTime, Date endTime) {
        List<AllotInBean> list = allotInBeanDao.queryBuilder()
                .where(AllotInBeanDao.Properties.CreateTime.between(startTime, endTime))
                .orderDesc(AllotInBeanDao.Properties.CreateTime, AllotInBeanDao.Properties.StockTransportCode)
                .list();
        return list;
    }
}